﻿using System;
using Infrastructure.Data.NH.Core.Util;
using NHibernate;
using Soleil.Domain.Core.Repositories;

namespace Infrastructure.Data.NH.Core.Repositories
{
    public class NHUnitOfWork : IUnitOfWork<ISession>
    {
        private bool disposed;
        private readonly ISession dbContext;

        public NHUnitOfWork()
        {
            dbContext = SessionManager.Instance.Session;
            dbContext.BeginTransaction();
        }

        public void Commit()
        {
            DbContext.Transaction.Commit();
        }

        public void Rollback()
        {
            DbContext.Transaction.Rollback();
        }

        public ISession DbContext
        { 
            get { return dbContext; }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    DbContext.Dispose();
                }
                disposed = true;
            }
        }
    }
}
